home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / timedops.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  7KB  |  196 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gobject
  5. import gtk
  6. import subprocess
  7. import threading
  8. from gettext import gettext as _
  9. from debug import *
  10.  
  11. class OperationCanceled(RuntimeError):
  12.     pass
  13.  
  14.  
  15. class Timed:
  16.     
  17.     def run(self):
  18.         pass
  19.  
  20.     
  21.     def cancel(self):
  22.         return False
  23.  
  24.  
  25.  
  26. class TimedSubprocess(Timed):
  27.     
  28.     def __init__(self, timeout = 60000, parent = None, show_dialog = True, **args):
  29.         self.subp = subprocess.Popen(**args)
  30.         self.output = dict()
  31.         self.io_source = []
  32.         self.watchers = 2
  33.         self.timeout = timeout
  34.         self.parent = parent
  35.         self.show_dialog = show_dialog
  36.         for f in [
  37.             self.subp.stdout,
  38.             self.subp.stderr]:
  39.             if f != None:
  40.                 source = gobject.io_add_watch(f, gobject.IO_IN | gobject.IO_HUP | gobject.IO_ERR, self.watcher)
  41.                 self.io_source.append(source)
  42.                 continue
  43.         
  44.         self.wait_window = None
  45.  
  46.     
  47.     def run(self):
  48.         if self.show_dialog:
  49.             self.wait_source = gobject.timeout_add(1000, self.show_wait_window)
  50.         
  51.         self.timeout_source = gobject.timeout_add(self.timeout, self.do_timeout)
  52.         gtk.main()
  53.         gobject.source_remove(self.timeout_source)
  54.         if self.show_dialog:
  55.             gobject.source_remove(self.wait_source)
  56.         
  57.         for source in self.io_source:
  58.             gobject.source_remove(source)
  59.         
  60.         if self.wait_window != None:
  61.             self.wait_window.destroy()
  62.         
  63.         return (self.output.get(self.subp.stdout, '').split('\n'), self.output.get(self.subp.stderr, '').split('\n'), self.subp.poll())
  64.  
  65.     
  66.     def do_timeout(self):
  67.         gtk.main_quit()
  68.         return False
  69.  
  70.     
  71.     def watcher(self, source, condition):
  72.         if condition & gobject.IO_IN:
  73.             buffer = self.output.get(source, '')
  74.             buffer += source.read()
  75.             self.output[source] = buffer
  76.         
  77.         return True
  78.  
  79.     
  80.     def show_wait_window(self):
  81.         wait = gtk.MessageDialog(self.parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CANCEL, _('Please wait'))
  82.         wait.connect('delete_event', (lambda : False))
  83.         wait.connect('response', self.wait_window_response)
  84.         if self.parent:
  85.             wait.set_transient_for(self.parent)
  86.         
  87.         wait.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  88.         wait.format_secondary_text(_('Gathering information'))
  89.         wait.show_all()
  90.         self.wait_window = wait
  91.         return False
  92.  
  93.     
  94.     def wait_window_response(self, dialog, response):
  95.         if response == gtk.RESPONSE_CANCEL:
  96.             self.cancel()
  97.         
  98.  
  99.     
  100.     def cancel(self):
  101.         if self.watchers > 0:
  102.             debugprint('Command canceled')
  103.             gtk.main_quit()
  104.             self.watchers = 0
  105.         
  106.         return False
  107.  
  108.  
  109.  
  110. class OperationThread(threading.Thread):
  111.     
  112.     def __init__(self, target = None, args = (), kwargs = { }):
  113.         threading.Thread.__init__(self)
  114.         self.setDaemon(True)
  115.         self.target = target
  116.         self.args = args
  117.         self.kwargs = kwargs
  118.         self.exception = None
  119.         self.result = None
  120.  
  121.     
  122.     def run(self):
  123.         
  124.         try:
  125.             debugprint('Calling %s' % self.target)
  126.             self.result = self.target(*self.args, **self.kwargs)
  127.             debugprint('Done')
  128.         except Exception:
  129.             e = None
  130.             debugprint('Caught exception %s' % e)
  131.             self.exception = e
  132.  
  133.  
  134.     
  135.     def collect_result(self):
  136.         if self.isAlive():
  137.             raise OperationCanceled()
  138.         self.isAlive()
  139.         if self.exception:
  140.             raise self.exception
  141.         self.exception
  142.         return self.result
  143.  
  144.  
  145.  
  146. class TimedOperation(Timed):
  147.     
  148.     def __init__(self, target, args = (), kwargs = { }, parent = None, show_dialog = False):
  149.         self.wait_window = None
  150.         self.parent = parent
  151.         self.show_dialog = show_dialog
  152.         self.thread = OperationThread(target = target, args = args, kwargs = kwargs)
  153.         self.thread.start()
  154.  
  155.     
  156.     def run(self):
  157.         if self.show_dialog:
  158.             wait = gtk.MessageDialog(self.parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CANCEL, _('Please wait'))
  159.             wait.connect('delete_event', (lambda : False))
  160.             wait.connect('response', self.wait_window_response)
  161.             if self.parent:
  162.                 wait.set_transient_for(self.parent)
  163.             
  164.             wait.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  165.             wait.format_secondary_text(_('Gathering information'))
  166.             wait.show_all()
  167.         
  168.         self.timeout_source = gobject.timeout_add(50, self.check_thread)
  169.         gtk.main()
  170.         gobject.source_remove(self.timeout_source)
  171.         if self.show_dialog:
  172.             wait.destroy()
  173.         
  174.         return self.thread.collect_result()
  175.  
  176.     
  177.     def check_thread(self):
  178.         if self.thread.isAlive():
  179.             return True
  180.         gtk.main_quit()
  181.         return False
  182.  
  183.     
  184.     def wait_window_response(self, dialog, response):
  185.         if response == gtk.RESPONSE_CANCEL:
  186.             self.cancel()
  187.         
  188.  
  189.     
  190.     def cancel(self):
  191.         debugprint('Command canceled')
  192.         gtk.main_quit()
  193.         return False
  194.  
  195.  
  196.